import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    
    let viewController = UIApplication.shared.windows.first!.rootViewController as! FlutterViewController
    
    print(".viewController:",viewController)

    let channel = FlutterMethodChannel(name:"samples.chenhang/utils",
                                       binaryMessenger:viewController.binaryMessenger)
    channel.setMethodCallHandler { (call, result) in
        print("call.method:"+call.method)
        if (call.method == "openAppMarket") {
            let url = URL(string: "itms-apps://itunes.apple.com/xy/app/foo/id414478124")
            print(url)
            UIApplication.shared.openURL(url!)
            
            //result(NSNumber(value: 0))
            result(Int(0))
        }else {
            result(FlutterError(code: "UNAVAILABLE",
                                    message: "Battery info unavailable",
                                    details: nil))
        }
    }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
